package com.zb.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.dto.DeviceDTO;
import com.zb.dto.QuotaAllInfo;
import com.zb.repository.DeviceRepository;
import com.zb.service.DeviceService;
import com.zb.service.TbQuotaService;
import com.zb.util.Pager;
import com.zb.util.ResultResponse;
import com.zb.vo.DeviceQuotaVO;
import com.zb.vo.DeviceSearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceServiceImpl  implements DeviceService {
    @Autowired
    private DeviceRepository deviceRepository;
    @Autowired
    private TbQuotaService tbQuotaService;
    @Override
    public void setStatus(String deviceId, Boolean status) {
        deviceRepository.updateStatus(deviceId,status);
    }

    @Override
    public void setTag(String deviceId, String tag) {
        deviceRepository.updateTag(deviceId,tag);
    }

    @Override
    public void setOnline(String deviceId, Boolean online) {
        deviceRepository.updateOnline(deviceId,online);
    }

    @Override
    public ResultResponse search(DeviceSearchVO deviceSearchVO) {
        List<DeviceDTO> list = deviceRepository.search(deviceSearchVO.getIndex(),
                deviceSearchVO.getSize(),deviceSearchVO.getDeviceId(),deviceSearchVO.getStatus(),
                deviceSearchVO.getTag());
        return ResultResponse.success(list);
    }

    @Override
    public boolean saveDeviceInfo(DeviceDTO deviceDTO) {
        //查询这个设备是否存在，这个设备的状态是否能用
        DeviceDTO device = deviceRepository.searchDeviceById(deviceDTO.getDeviceId());

        if (device!=null && !device.getStatus()){
            return false;
        }
        if (device  == null){
            deviceRepository.addDevice(deviceDTO);
        }else {
            deviceRepository.updateAlarm(deviceDTO.getDeviceId(),deviceDTO.getAlarm(),
                    deviceDTO.getAlarmName(),deviceDTO.getLevel());
        }

        return true;
    }

    @Override
    public Pager<DeviceQuotaVO> queryDeviceQuota(Integer page, Integer size, String devceId, String tag, Integer state) {
        //es中查询设备信息
        List<DeviceDTO> deviceDTOList = deviceRepository.search(page.intValue(), size.intValue(), devceId, state, tag);
        List<DeviceQuotaVO> deviceQuotaVOList = new ArrayList<>();
        //为设备查找最近的指标
        for (DeviceDTO deviceDTO : deviceDTOList) {
            List<QuotaAllInfo> lastQuotaList = tbQuotaService.getLastQuotaList(deviceDTO.getDeviceId());
            DeviceQuotaVO deviceQuotaVO = new DeviceQuotaVO();
            deviceQuotaVO.setDeviceId(deviceDTO.getDeviceId());
            deviceQuotaVO.setOnline(deviceDTO.getOnline());
            deviceQuotaVO.setLevel(deviceDTO.getLevel());
            deviceQuotaVO.setQuotaList(lastQuotaList);
            deviceQuotaVOList.add(deviceQuotaVO);
        }
        Pager<DeviceQuotaVO> pager = new Pager<>();
        pager.setIndex(page.intValue());
        pager.setSize(size.intValue());
        pager.setData(deviceQuotaVOList);
        return pager;
    }
}
